package com.ai.exam.controller;


import com.ai.exam.dto.WxModel;
import com.ai.exam.model.UserHelp;
import com.ai.exam.service.UserHelpService;
import com.ai.exam.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* Created by CodeGenerator on 2018/06/01.
*/
@RestController
@RequestMapping("/user/help")
@Api(value = "用户接口",tags = {"活动用户管理"})
public class UserHelpController {
    private Logger logger = LoggerFactory.getLogger(UserHelpController.class);
    @Resource
    private UserHelpService userHelpService;

    @PostMapping("addUserPrize")
    @ApiOperation(value = "用户被助力（领取流量）" ,notes = "参数：openId，openIdHelp，telnumHelp")
    public synchronized Result addUserPrize(@RequestBody UserHelp userHelp){
        return userHelpService.addUserPrize(userHelp);
    }

    @PostMapping("userInit")
    @ApiOperation(value = "用户首次登录参与活动，记录用户" ,notes = "参数：openId，telnum")
    public Result userInit(@RequestBody UserHelp userHelp){
        logger.info("初始化用户，参数：{}",userHelp);
        UserHelp rs = userHelpService.findById(userHelp.getOpenId());
        if(rs!=null){
            logger.info("用户已存在");
            return Result.fail("用户已存在");
        }else{
            int i = userHelpService.save(userHelp);
            logger.info("用户插入结果：{}",i);
            return Result.success(i);
        }
    }

    @PostMapping("selUser")
    @ApiOperation(value = "按openId查询用户" ,notes = "参数：openId")
    public Result selUser(@RequestBody WxModel wxModel){
        logger.info("按openId查询用户,参数：{}",wxModel);
        return userHelpService.selUser(wxModel.getOpenId());
    }
}
